package cn.gson.crm.control.dept;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.control.base.BaseServlet;
import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.entity.Dept;

/**
 * 更新部门
 * */

@WebServlet("/dept/update")
public class DeptUpdateServlet extends BaseServlet{
	private String formJsp = "dept/manager";
	private DeptDao deptDao = new DeptDao();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("did");
		Dept dept = deptDao.getDeptByDeptId(id);
		List<Dept> allDeptList;
		allDeptList = deptDao.getList();
		request.setAttribute("allDeptList", allDeptList);
		request.setAttribute("d", dept);
		forward("dept/dept_update.jsp");
	}
	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int id = Integer.parseInt(request.getParameter("id"));
		String deptName = request.getParameter("deptName");
		String deptManager = request.getParameter("deptManager");
		String deptParent = request.getParameter("addDeptParent");
		// 验证
					if (deptName == null || deptName.isEmpty()) {
						errorForward("名称不能为空！", formJsp);
						return;
					}
					
					if (deptParent == null || deptParent.isEmpty()) {
						errorForward("上属部门不能为空！", formJsp);
						return;
					}
					
					Dept dept = new Dept();
					
					dept.setDeptId(id);
					dept.setDeptManager(deptManager);
					dept.setDeptName(deptName);
					dept.setDeptParent(deptParent);
					deptDao.saveDept(dept);
					response.sendRedirect("manager");
	}
}
